$(document).ready(function () {
    // 重新计算屏幕
    resizeScreen();
    // 个人菜单侧边栏显示子菜单
    $('.collapse').collapse('show');

    // 主页幻灯片播放
    $('.carousel').carousel();

    // 个人菜单侧边栏点击事件
    $('#menuFunction').on('shown.bs.collapse', function(){
        $(this).parent().children('a').children('i').removeClass('fa-angle-down');
        $(this).parent().children('a').children('i').addClass('fa-angle-up');
    });

    $('#menuFunction').on('hidden.bs.collapse', function(){
        $(this).parent().children('a').children('i').removeClass('fa-angle-up');
        $(this).parent().children('a').children('i').addClass('fa-angle-down');
    });

    // validate添加验证规则
    // 手机号码
    jQuery.validator.addMethod("mobile", function(value, element) {  
        var length = value.length;
        var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;  
        return this.optional(element) || (length == 11 && mobile.test(value));  
    }, "请正确填写您的手机号码");

    jQuery.validator.addMethod("lettersnumbric", function(value, element) {  
        var i = /^[a-zA-Z0-9]+$/;
        return this.optional(element) || i.test(value);
    }, "格式不正确");

    // 注销动作
    $('#logoutAction').click(function(){
        $.ajax({
            url: '/member/logout/',
            type: 'POST',
            dataType: 'JSON',
            success: function(ret) {
                if(ret.code == 200) {
                    // 注销成功后返回到首页
                    location.href = '/';
                }
            }
        });
    });

    // 登录验证
    $('#loginForm').validate({
        rules: {
            username: {
                required: true,
                minlength: 6,
                maxlength: 20,
                lettersnumbric: true,
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20,
                lettersnumbric: true,
            }
        },
        messages: {
            username: {
                required: '请填写用户名',
                minlength: '用户名最短6位',
                maxlength: '用户名最长20位',
                lettersnumbric: '用户名只能由字母和数字组成'
            },
            password: {
                required: '请填写密码',
                minlength: '密码最短6位',
                maxlength: '密码最长20位',
                lettersnumbric: '密码只能由字母和数字组成'
            }
        },
        errorClass: 'validate-error',
        submitHandler: function(){
            var password = $('#lPassword').val();
            var encodePass = Base64.encode(password);
            $('#lPassword').val(encodePass);

            $('#loginForm').ajaxSubmit({
                url: '/member/login/',
                type: 'POST',
                success: function(ret) {
                    if(ret.code == 200) {
                        swal({
                            title: '成功',
                            text: '登录成功',
                            type: "success",
                            button: '确认'
                        }).then((p) => {
                            location.reload();
                        });
                    }
                },
                error: function(ret) {
                    $('#lPassword').val(password);
                }
            });
        }
    });

    // 注册验证
    $('#registerForm').validate({
        rules: {
            username: {
                required: true,
                minlength: 6,
                maxlength: 20,
                lettersnumbric: true,
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20,
                lettersnumbric: true,
            },
            repassword: {
                required: true,
                minlength: 6,
                maxlength: 20,
                lettersnumbric: true,
                equalTo: "#rPassword",
            }
        },
        messages: {
            username: {
                required: '请填写用户名',
                minlength: '用户名最短6位',
                maxlength: '用户名最长20位',
                lettersnumbric: '用户名只能由字母和数字组成'
            },
            password: {
                required: '请填写密码',
                minlength: '密码最短6位',
                maxlength: '密码最长20位',
                lettersnumbric: '密码只能由字母和数字组成',
            },
            repassword: {
                required: '请填写密码',
                minlength: '密码最短6位',
                maxlength: '密码最长20位',
                lettersnumbric: '密码只能由字母和数字组成',
                equalTo: '两次密码不一致',
            }
        },
        errorClass: 'validate-error',
        submitHandler: function(){
            var password = $('#rPassword').val();
            var encodePass = Base64.encode(password);
            $('#rPassword').val(encodePass);

            $('#registerForm').ajaxSubmit({
                url: '/member/register/',
                type: 'post',
                success: function(ret) {
                    if(ret.code == 200) {
                        swal({
                            title: '成功',
                            text: '注册成功，请登录',
                            type: "success",
                            button: '确认'
                        }).then((p) => {
                            $('#registerModel').modal('hide');
                            $('#loginModel').modal('show');
                        });
                    }
                }
            });
        },
    });
});

function resizeScreen() {
    
    var screenWidth = $(document).width();
    var screenHeight = $(document).height();

    var leftBarWidth = $('.container-fluid .left-bar').width();
    var rightBarWidth = screenWidth - leftBarWidth;

    $('.container-fluid .right-bar').width(rightBarWidth);
    $('.container-fluid .right-bar').height(screenHeight);
    $('.container-fluid .left-bar').height(screenHeight);
}


